import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_learn_vi/src/page/study/controller/video_details_local_controller.dart';
import 'package:flutter_learn_vi/src/utils/log_util.dart';
import 'package:get/get.dart';

import '../../../bean/bean_video_course.dart';
import '../../home/new/controller/HomeMainV2PageController.dart';
import '../course/video_progress_request_model.dart';

class CourseVideoDetailsController extends GetxController
    with VideoProgressRequestModel {
  int trainId;
  int currentPlayVideoId;

  CourseVideoDetailsController(this.trainId);

  int currentInSeconds = 0;

  Future<bool> clickStopVideo({
    int videoId,
    int inMilliseconds,
    int videoCourseId,
    bool finish = false,
  }) async {
    await updateVideo(
        trainId: trainId,
        videoId: videoId,
        finish: finish,
        videoCourseId: videoCourseId,
        current: inMilliseconds);
    return true;
  }

  Future<bool> clickStopVideoToHome({
    int videoId,
    int inMilliseconds,
    int videoCourseId,
    bool finish = false,
  }) async {
    if (Get.isRegistered<HomeMainV2PageController>()) {
      Get.find<HomeMainV2PageController>().homeUpdateVideo(
          trainId: trainId,
          videoId: videoId,
          finish: finish,
          videoCourseId: videoCourseId,
          current: inMilliseconds);
    }
    return true;
  }

  //点击播放视频时调用
  Future<UserStudyCourseBean> clickPlayVideo({int videoId}) async {
    if (currentPlayVideoId != null && currentPlayVideoId != 0) {
      //如果当前有正在播放的视频 要停止播放
      //更新这个视频的播放进度
    }
    LogUtils.e("开始播放 $videoId");
    currentPlayVideoId = videoId;

    return null;
  }

  ///点击切换视频的时候来调用
  Future<bool> changPlayVideo() async {
    if (currentPlayVideoId != null) {
      if (Get.isRegistered<VideoDetailsLocalController>(
          tag: "$currentPlayVideoId")) {
        EasyLoading.show(status: '加载中...');
        await Get.find<VideoDetailsLocalController>(tag: "$currentPlayVideoId")
            .closePageUpdateProgress();
        EasyLoading.dismiss();
      } else {
        LogUtils.e("视频切换 VideoDetailsLocalController 未注册");
      }
    }
    return true;
  }

  //获取用户当前的播放时间  点击播放的时候
  Future<UserStudyCourseBean> getcCurrentVideoProgress({int videoId}) async {
    UserStudyCourseBean userStudyCourseBean =
        await getCurrentVideo(videoId: videoId, trainId: trainId);
    if (userStudyCourseBean != null) {
      currentInSeconds = userStudyCourseBean.studySecond;
    }
    return userStudyCourseBean;
  }

  void closePage() async {
    if (0 == 0) {
      if (Get.isRegistered<VideoDetailsLocalController>(
          tag: "$currentPlayVideoId")) {
        // EasyLoading.show(status: '加载中...');
        await Get.find<VideoDetailsLocalController>(tag: "$currentPlayVideoId")
            .closePageUpdateProgress();
        // EasyLoading.dismiss();
      } else {
        LogUtils.e("页面关闭 VideoDetailsLocalController 未注册");
      }
      //获取当前正在播放的视频
      Get.back(result: true);
      return;
    }
    dynamic result = await Get.defaultDialog(
        title: "提示",
        content: Text("确认关闭页面吗?"),
        textCancel: "取消",
        textConfirm: "确认",
        onConfirm: () {
          Get.back(result: true);
        });
    if (result != null && result) {
      if (Get.isRegistered<VideoDetailsLocalController>(
          tag: "$currentPlayVideoId")) {
        // EasyLoading.show(status: '加载中...');
        await Get.find<VideoDetailsLocalController>(tag: "$currentPlayVideoId")
            .closePageUpdateProgress();
        // EasyLoading.dismiss();
      } else {
        LogUtils.e("页面关闭 VideoDetailsLocalController 未注册");
      }
      //获取当前正在播放的视频
      Get.back(result: true);
    }
  }
}
